<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <style>
        .layui-table-cell {
            height: 35px !important;
        }
    </style>
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button  data-type="add" class="layui-btn layui-btn-sm" lay-event="save">
            <i class="layui-icon">&#xe608;</i> 添加教师信息
        </button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>

    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="${pageContext.request.contextPath}/layui/layui.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath}/js/jquery-3.3.1.min.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use('table', function(){

        var table = layui.table;
        var pn ;//当前页
        var pc ;//当前页分几条数据


        table.render({
            elem: '#test'
            ,url:'${pageContext.request.contextPath}/TeacherPagingAjaxServlet.action'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '教师表数据'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'tid', title:'教师编号',  fixed: 'left', unresize: true, sort: true}
                ,{field:'tname', title:'教师姓名', edit: 'text'}
                ,{field:'tsal', title:'工资',  edit: 'text', sort: true}
                ,{field:'tsex', title:'性别', sort: true}
                ,{field:'tdate', title:'入职时间',sort:true}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo',unresize: true, width:130}
            ]]
            ,page: true,
            limit:10,
            request: {
                pageName: 'pageNumber' //页码的参数名称，默认：page
                ,limitName: 'pageCount' //每页数据量的参数名，默认：limit
            }
            ,response: {
                statusName: 'code'
                ,msgName:'msg'
                ,countName: 'total' //数据总数的字段名称，默认：count
                ,dataName: 'data' //数据列表的字段名称，默认：data
            },done:function(res, curr, count){
                pc = res.pageCount; //得到当前页分几条数据
                pn = curr ;//得到当前页
            }

        });






        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;
                case 'save':
                    layer.open({
                        type: 1 //Page层类型
                        ,skin: 'layui-layer-molv'
                        ,area: ['500px', '500px']
                        ,title: ['添加教师信息','font-size:18px']
                        ,btn: ['确定', '取消']
                        ,shadeClose: true
                        ,shade: 0 //遮罩透明度
                        ,maxmin: true //允许全屏最小化
                        ,content:$('#box2')  //弹窗id
                        ,yes:function(index,layero){
                            $.post("${pageContext.request.contextPath}/TeacherSaveAjaxServlet.action",$("#box2").serialize(),function(data){

                                //根据后台返回的参数，来进行判断
                                if(data.flag){
                                    layer.alert('编辑成功',{icon:1,title:'提示'},function(i){
                                        layer.close(i);
                                        layer.close(index);//关闭弹出层
                                        $("#box2")[0].reset()//重置form
                                    });
                                    table.reload('test',{//重载表格
                                        page:{
                                            curr:pn,
                                            count:pc
                                        }
                                    })
                                }

                            },"json");

                        }
                    });
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
        });





        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.post("TeacherDeleteAjaxServlet.action","tid="+data.tid,function(data){
                        if(data.flag==true){
                            table.reload('test',{//重载表格
                                page:{
                                    curr:pn,
                                    count:pc
                                }
                            })
                        }else{
                            layer.msg("库中还有该教师的学生哦！");
                        }
                    },"json");
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){

                //------------------------------------------------
                layer.open({
                    type: 1 //Page层类型
                    ,skin: 'layui-layer-molv'
                    ,area: ['500px', '500px']
                    ,title: ['编辑教师信息','font-size:18px']
                    ,btn: ['确定', '取消']
                    ,shadeClose: true
                    ,shade: 0 //遮罩透明度
                    ,maxmin: true //允许全屏最小化
                    ,content:$('#box1')  //弹窗id
                    ,success:function(layero,index){
                        $('#tid').val(data.tid);
                        $('#tname').val(data.tname);
                        $('#tsal').val(data.tsal);
                        $("#box1").find("input:radio[value='"+data.tsex+"']").prop("checked",true);
                        $("#tdate").val(data.tdate);
                        form.render('radio');
                    },yes:function(index,layero){
                        $.post("${pageContext.request.contextPath}/TeacherUpdateAjaxServlet.action",$("#box1").serialize(),function(data){

                            //根据后台返回的参数，来进行判断
                            if(data.flag){
                                layer.alert('编辑成功',{icon:1,title:'提示'},function(i){
                                    layer.close(i);
                                    layer.close(index);//关闭弹出层
                                    $("#box1")[0].reset()//重置form
                                })
                                table.reload('test',{//重载表格
                                    page:{
                                        curr:pn,
                                        count:pc
                                    }
                                })
                            }

                        },"json");

                    }
                });
                //--------------------------------------------------------
            }
        });


    });


</script>


<form class="layui-form" action="" id="box1" style="display: none;">
    <input name="tid" id="tid" type="hidden">
    <div class="layui-form-item" style="margin-top:50px; width: 300px;">
        <label class="layui-form-label">教师姓名：</label>
        <div class="layui-input-block">
            <input type="text" name="tname" id="tname" lay-verify="title" autocomplete="off" placeholder="请输入教师姓名" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" style="width: 300px;">
        <label class="layui-form-label">工资：</label>
        <div class="layui-input-block">
            <input type="text" name="tsal" id="tsal" lay-verify="title" autocomplete="off" placeholder="请输入工资" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">性别：</label>
        <div class="layui-input-block">
            <input type="radio" name="tsex" value="男" title="男">
            <input type="radio" name="tsex" value="女" title="女">
        </div>
    </div>

    <div class="layui-inline" style="width: 300px;">
        <label class="layui-form-label">入职时间:</label>
        <div class="layui-input-inline">
            <input type="text" name="tdate" id="tdate" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
        </div>
    </div>
</form>
<form class="layui-form" action="" id="box2" style="display: none;">
    <input name="tid" id="tid2" type="hidden">
    <div class="layui-form-item" style="margin-top:50px; width: 300px;">
        <label class="layui-form-label">教师姓名：</label>
        <div class="layui-input-block">
            <input type="text" name="tname" id="tname2" lay-verify="title" autocomplete="off" placeholder="请输入教师姓名" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" style="width: 300px;">
        <label class="layui-form-label">工资：</label>
        <div class="layui-input-block">
            <input type="text" name="tsal" id="tsal2" lay-verify="title" autocomplete="off" placeholder="请输入工资" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">性别：</label>
        <div class="layui-input-block">
            <input type="radio" name="tsex" value="男" title="男" checked="checked">
            <input type="radio" name="tsex" value="女" title="女">
        </div>
    </div>

    <div class="layui-inline" style="width: 300px;">
        <label class="layui-form-label">入职时间:</label>
        <div class="layui-input-inline">
            <input type="text" name="tdate" id="tdate2" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
        </div>
    </div>
</form>
</body>
</html>

<script>
    var form;
    var layer;
    layui.use(['form', 'layedit', 'laydate'], function(){
        laydate = layui.laydate;
        form = layui.form;
        layer = layui.layer;

        //日期
        laydate.render({
            elem: '#tdate'
        });
        laydate.render({
            elem: '#tdate2'
        });
    });
</script>
